/**
 * 作者: wgx
 * 日期: 2024-11-04 17:29:55
 * 备注: 用户邮件表
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.Common.PBUserMail;
import com.motu.vertx.module.utility.model.BaseModel;
import com.motu.vertx.module.utility.model.Model;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;

public class UserMailModel extends BaseModel implements Model {
    public static final String LISTKEY = "user_id";
    public static final int REDIS_EXPIRE_TIME = 0;// redis过期时间
    public static final int UPDATE_DB_TYPE = UPDATE_DB_TYPE_QUEUE;// 数据入库类型 队列入库
    public static final boolean ORDER_LIST = false;// 是否需要额外的有序列表

    protected long id; // id
    protected long userId; // 用户ID
    protected byte mailType; // 邮件类型 (0普通 1重要)
    protected String title = ""; // 标题
    protected String content = ""; // 内容
    protected long addTime; // 创建时间
    protected byte isRead; // 是否已读
    protected String item = ""; // 附件
    protected byte isGet; // 是否已领取
    protected int logType; // 日志类型
    protected String titleReplace = ""; // 标题替代符的文字 (多个用|分开)
    protected String contentReplace = ""; // 内容替代符的文字 (多个用|分开)

    public void reset() {
    }

    protected String getKey() {
        return "UserMail." + id;
    }

    protected String getListKey() {
        return "UserMail." + userId;
    }

    protected String getListItemKey() {
        return id + "";
    }

    public void doCreate() {
        setTableId();
        insert();
    }

    public void setTableId() {
        putId(incrTableId("user_mail"));
    }

    public JsonObject insert() {
        return super.insert(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    public void delete() {
        super.delete(getKey(), id, UPDATE_DB_TYPE, userId, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    /**
     * 根据入库类型更新
     */
    public JsonObject update() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, userId, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    /**
     * 实时入队列
     */
    public JsonObject updateQueueNow() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    public JsonObject getAllObj(){
        JsonObject obj = new JsonObject();
        obj.put("id", id); // id
        obj.put("userId", userId); // 用户ID
        obj.put("mailType", mailType); // 邮件类型 (0普通 1重要)
        obj.put("title", title); // 标题
        obj.put("content", content); // 内容
        obj.put("addTime", addTime); // 创建时间
        obj.put("isRead", isRead); // 是否已读
        obj.put("item", item); // 附件
        obj.put("isGet", isGet); // 是否已领取
        obj.put("logType", logType); // 日志类型
        obj.put("titleReplace", titleReplace); // 标题替代符的文字 (多个用|分开)
        obj.put("contentReplace", contentReplace); // 内容替代符的文字 (多个用|分开)
        return obj;
    }

    public PBUserMail.Builder takeInitBuilder() {
        PBUserMail.Builder sb = PBUserMail.newBuilder();
        if (id != 0) {
            sb.setId(id);
        }
        if (mailType != 0) {
            sb.setMailType(mailType);
        }
        if (!title.isEmpty()) {
            sb.setTitle(title);
        }
        if (!content.isEmpty()) {
            sb.setContent(content);
        }
        if (addTime != 0) {
            sb.setAddTime(addTime);
        }
        if (isRead != 0) {
            sb.setIsRead(isRead);
        }
        if (isGet != 0) {
            sb.setIsGet(isGet);
        }
        return sb;
    }

    public PBUserMail.Builder takeBuilder() {
        if (updateObj == null || updateObj.isEmpty()) {
            return takeInitBuilder();
        }
        PBUserMail.Builder sb = PBUserMail.newBuilder();
        if (isFieldUpdate("id")) {
            sb.setId(id);
        }
        if (isFieldUpdate("mailType")) {
            sb.setMailType(mailType);
        }
        if (isFieldUpdate("title")) {
            sb.setTitle(title);
        }
        if (isFieldUpdate("content")) {
            sb.setContent(content);
        }
        if (isFieldUpdate("addTime")) {
            sb.setAddTime(addTime);
        }
        if (isFieldUpdate("isRead")) {
            sb.setIsRead(isRead);
        }
        if (isFieldUpdate("isGet")) {
            sb.setIsGet(isGet);
        }
        return sb;
    }

    public long getId() {
        return id;
    }

    public void putId(long id) {
        if (this.id != id) {
            this.id = id;
            updateRedisHashField(getKey(), "id", this.id);
        }
    }

    public void addId(long value) {
        this.id += value;
        updateRedisHashField(getKey(), "id", this.id);
    }

    public long getUserId() {
        return userId;
    }

    public void putUserId(long userId) {
        if (this.userId != userId) {
            this.userId = userId;
            updateRedisHashField(getKey(), "userId", this.userId);
        }
    }

    public void addUserId(long value) {
        this.userId += value;
        updateRedisHashField(getKey(), "userId", this.userId);
    }

    public byte getMailType() {
        return mailType;
    }

    public void putMailType(byte mailType) {
        if (this.mailType != mailType) {
            this.mailType = mailType;
            updateRedisHashField(getKey(), "mailType", this.mailType);
        }
    }

    public void addMailType(byte value) {
        this.mailType += value;
        updateRedisHashField(getKey(), "mailType", this.mailType);
    }

    public String getTitle() {
        return title;
    }

    public void putTitle(String title) {
        if (!this.title.equals(title)) {
            this.title = title;
            updateRedisHashField(getKey(), "title", this.title);
        }
    }

    public String getContent() {
        return content;
    }

    public void putContent(String content) {
        if (!this.content.equals(content)) {
            this.content = content;
            updateRedisHashField(getKey(), "content", this.content);
        }
    }

    public long getAddTime() {
        return addTime;
    }

    public void putAddTime(long addTime) {
        if (this.addTime != addTime) {
            this.addTime = addTime;
            updateRedisHashField(getKey(), "addTime", this.addTime);
        }
    }

    public void addAddTime(long value) {
        this.addTime += value;
        updateRedisHashField(getKey(), "addTime", this.addTime);
    }

    public byte getIsRead() {
        return isRead;
    }

    public void putIsRead(byte isRead) {
        if (this.isRead != isRead) {
            this.isRead = isRead;
            updateRedisHashField(getKey(), "isRead", this.isRead);
        }
    }

    public void addIsRead(byte value) {
        this.isRead += value;
        updateRedisHashField(getKey(), "isRead", this.isRead);
    }

    public String getItem() {
        return item;
    }

    public void putItem(String item) {
        if (!this.item.equals(item)) {
            this.item = item;
            updateRedisHashField(getKey(), "item", this.item);
        }
    }

    public byte getIsGet() {
        return isGet;
    }

    public void putIsGet(byte isGet) {
        if (this.isGet != isGet) {
            this.isGet = isGet;
            updateRedisHashField(getKey(), "isGet", this.isGet);
        }
    }

    public void addIsGet(byte value) {
        this.isGet += value;
        updateRedisHashField(getKey(), "isGet", this.isGet);
    }

    public int getLogType() {
        return logType;
    }

    public void putLogType(int logType) {
        if (this.logType != logType) {
            this.logType = logType;
            updateRedisHashField(getKey(), "logType", this.logType);
        }
    }

    public void addLogType(int value) {
        this.logType += value;
        updateRedisHashField(getKey(), "logType", this.logType);
    }

    public String getTitleReplace() {
        return titleReplace;
    }

    public void putTitleReplace(String titleReplace) {
        if (!this.titleReplace.equals(titleReplace)) {
            this.titleReplace = titleReplace;
            updateRedisHashField(getKey(), "titleReplace", this.titleReplace);
        }
    }

    public String getContentReplace() {
        return contentReplace;
    }

    public void putContentReplace(String contentReplace) {
        if (!this.contentReplace.equals(contentReplace)) {
            this.contentReplace = contentReplace;
            updateRedisHashField(getKey(), "contentReplace", this.contentReplace);
        }
    }

}